package com.atguigu.tingshu.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 订单微服务超时取消订单
 */
@Configuration
public class OrderRabbitConfig {

    //定义订单取消交换机
    @Bean
    public Exchange OrderCancelExchange(){
        return ExchangeBuilder.directExchange("OrderInfo_Cancel_Exchange").build();
    }
    /**
     * 创建死信队列
     */
    @Bean
    public Queue OrderdeadQueue(){
        return QueueBuilder.durable("Order_Self_Cancel_Queue")
                .withArgument("x-dead-letter-exchange","OrderInfo_Cancel_Exchange")
                .withArgument("x-dead-letter-routing-key","TimeoutCancel").build();
    }


    /**
     * 死信队列绑定交换机
     */
    @Bean
    public Binding OrderdeadQueueBind(@Qualifier(value = "OrderdeadQueue") Queue OrderSelfCancelQueue,
                                      @Qualifier(value = "OrderCancelExchange") Exchange OrderCancelExchange){
        return BindingBuilder.bind(OrderSelfCancelQueue).to(OrderCancelExchange).with("selfCancel").noargs();
    }

    /**
     * 创建普通队列
     */
    @Bean
    public Queue OrderCommonlQueue(){
        return QueueBuilder.durable("Order_TimeOut_Cancel_Queue").build();
    }

    /**
     * 普通队列绑定交换机
     * @param OrderTimeOutCancelQueue
     * @param OrderCancelExchange
     * @return
     */
    @Bean
    public Binding OrderCommonlQueueBind(@Qualifier(value = "OrderCommonlQueue") Queue OrderTimeOutCancelQueue,
                                               @Qualifier(value = "OrderCancelExchange") Exchange OrderCancelExchange){
        return BindingBuilder.bind(OrderTimeOutCancelQueue).to(OrderCancelExchange).with("TimeoutCancel").noargs();
    }


}
